from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from page_objects.base_page import BasePage
from utils.log_utils import logger


class CategoryListPage(BasePage):

    _ADD_ELEMENT= (By.XPATH, "//*[text()='添加']")
    _ADD_SUCESS = (By.XPATH, '//*[text()="创建成功"]')
    _DELETE_SUCESS = (By.XPATH, '//*[text()="删除成功"]')
    '''类目列表页面：点击添加'''
    def click_add(self):
        # 点击'添加'按钮
        self.do_find(self._ADD_ELEMENT).click()
        # ==》创建类目页面
        from page_objects.category_creat_page import CategoryCreatPage
        return CategoryCreatPage(self.driver)


    '''类目列表页面：获取操作结果'''
    def get_operate_result(self):
        # 获取冒泡消息文本
        # res = self.do_find(self._ADD_SUCESS).text
        element = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(self._ADD_SUCESS))
        # ==》返回消息文本
        msg = element.text
        logger.info(f"冒泡消息是：{msg}")
        return msg

    def delete_category(self, category_name):
        # 数据的清理一定要放在断言操作之后完成，要不然可能会影响断言结果
        self.do_find(By.XPATH, f"//*[text()='{category_name}']/../..//*[text()='删除']").click()

        return CategoryListPage(self.driver)

    def get_delete_result(self):
        # 获取冒泡消息文本
        # res = self.do_find(self._ADD_SUCESS).text
        element = WebDriverWait(self.driver, 10).\
            until(expected_conditions.
                  visibility_of_element_located(self._DELETE_SUCESS))
        # ==》返回消息文本
        msg = element.text
        logger.info(f"冒泡消息是：{msg}")
        return msg






